package com.lb.shopguide.ui.fragment.boss.market;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.widget.TextView;

import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.lb.shopguide.R;
import com.lb.shopguide.adapter.viewpager.AdapterMarketing;
import com.lb.shopguide.base.BaseCommonFragment;
import com.lb.shopguide.constants.AppConstant;
import com.lb.shopguide.event.boss.ShowTipEvent;
import com.lb.shopguide.event.jump.JumpMyActivityFromMarketEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

public class FragmentMarketing extends BaseCommonFragment {
    @BindView(R.id.stb_market)
    SlidingTabLayout stbMarket;
    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.viewPager)
    ViewPager mViewPager;

    private AdapterMarketing mAdapterMarket;

    private String[] mTitles = {"营销模版", "我的活动"};
    //不需要显示，但是资源id必须正确
    private boolean gotoActivityFlag = false;

    public static FragmentMarketing newInstance() {
        return new FragmentMarketing();
    }

    public static FragmentMarketing newInstance(boolean gotoMyActivity) {
        FragmentMarketing fragmentMarketing = new FragmentMarketing();
        Bundle bundle = new Bundle();
        bundle.putBoolean(AppConstant.GOTO_MYACTIVITY_FLAG, gotoMyActivity);
        fragmentMarketing.setArguments(bundle);
        return fragmentMarketing;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_marketing;
    }

    @Override
    public void initData() {
        Bundle bundle = getArguments();
        if (bundle.containsKey(AppConstant.GOTO_MYACTIVITY_FLAG)) {
            gotoActivityFlag = bundle.getBoolean(AppConstant.GOTO_MYACTIVITY_FLAG);
        }
        EventBus.getDefault().register(this);
    }

    @Override
    public void initView() {
        mAdapterMarket = new AdapterMarketing(getChildFragmentManager(), mTitles);
        mViewPager.setAdapter(mAdapterMarket);
        stbMarket.setViewPager(mViewPager, mTitles);
        if (gotoActivityFlag) {
            stbMarket.setCurrentTab(AppConstant.SECOND, true);
        } else {
            stbMarket.updateTabSelection(AppConstant.FIRST);
        }
        stbMarket.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
//                showHideFragment(mFragments[position], mFragments[curPos]);
//                ctbMarket.setCurrentTab(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
    }

    @Override
    public void finishRefresh() {

    }

    @Override
    public void sessionOutOfDate() {

    }

    @Override
    public void requestError(String error) {

    }

    @Override
    public String getPageTag() {
        return FragmentMarketing.class.getName();
    }

    @OnClick(R.id.tv_left)
    void leftClick() {
        pop();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void jumpActivity(JumpMyActivityFromMarketEvent event) {
        stbMarket.setCurrentTab(AppConstant.SECOND, true);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void showTip(ShowTipEvent event) {
        if (event.isFlag()) {
            stbMarket.showIcon(AppConstant.FIRST, true);
        } else {
            stbMarket.showIcon(AppConstant.FIRST, false);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

}
